package com.api.service.impl;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.api.common.reqres.Request;
import com.api.common.reqres.Response;
import com.api.component.BaseDataComponent;
import com.api.component.RequestComponent;
import com.api.component.ResponseComponent;
import com.api.mapper.LeaveMapper;
import com.api.model.entity.Leave;
import com.api.model.pojo.LeaveInfo;
import com.api.model.request.ApplicationLeaveRequest;
import com.api.model.request.CancelLeaveRequest;
import com.api.model.request.NullRequest;
import com.api.model.response.LeaveResponse;
import com.api.model.response.NullResponse;
import com.api.service.LeaveService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfp
 * @since 2019-10-14
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {
	@Autowired
	private RequestComponent requestComponent;
	@Autowired
	private ResponseComponent responseComponent;
	@Autowired
	private LeaveMapper leaveMapper;
	@Autowired
	private BaseDataComponent baseDataComponent;

	@Override
	public Response<LeaveResponse> leave(Request<NullRequest> request) {
		// TODO Auto-generated method stub
		String userid = request.getHead().getUserid();
		LambdaQueryWrapper<Leave> queryWrapper = new LambdaQueryWrapper<Leave>();
		queryWrapper.eq(Leave::getUserid, userid).orderByDesc(Leave::getCreatetime);
		List<Leave> leaveList = leaveMapper.selectList(queryWrapper);
		LeaveResponse response = new LeaveResponse();
		// 获取请假记录
		if (!CollectionUtils.isEmpty(leaveList)) {
			List<LeaveInfo> leaveInfoList = leaveList.stream().map(e -> {
				LeaveInfo LeaveInfo = new LeaveInfo();
				LeaveInfo.setOid(e.getOid());
				if (e.getIscancel()) {
					LeaveInfo.setLeaveText("已撤销");
				} else {
					LeaveInfo.setLeaveText("已请假");
				}
				try {
					Date leaveDate = DateUtils.parseDate(e.getLeavedate(), "yyyy-MM-dd");
					if (leaveDate.before(new Date())) {
						LeaveInfo.setCanCancel(false);
					} else {
						if (e.getIscancel()) {
							LeaveInfo.setCanCancel(false);
						} else {
							LeaveInfo.setCanCancel(true);
						}
					}
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
				LeaveInfo.setLeavedate(e.getLeavedate());
				return LeaveInfo;
			}).collect(Collectors.toList());
			response.setLeaveInfoList(leaveInfoList);
		}
		return responseComponent.toSuccess(response);
	}

	@Override
	public Response<NullResponse> applicationLeave(Request<ApplicationLeaveRequest> request) {
		// TODO Auto-generated method stub
		requestComponent.checkBody(request.getBody(), ApplicationLeaveRequest.class);
		String userid = request.getHead().getUserid();
		ApplicationLeaveRequest applicationLeaveRequest = request.getBody();
		Leave entity = new Leave();
		entity.setUserid(Long.valueOf(userid));
		entity.setLeavedate(applicationLeaveRequest.getLeavedate());
		entity.setLeavereason(applicationLeaveRequest.getLeavereason());
		entity.setIscancel(false);
		entity.setCreateuser(userid);
		entity.setCreatetime(LocalDateTime.now());
		leaveMapper.insert(entity);
		return responseComponent.toSuccess(null);
	}

	@Override
	public Response<NullResponse> cancelLeave(Request<CancelLeaveRequest> request) {
		// TODO Auto-generated method stub
		requestComponent.checkBody(request.getBody(), CancelLeaveRequest.class);
		CancelLeaveRequest cancelLeaveRequest = request.getBody();
		LambdaUpdateWrapper<Leave> updateWrapper = new LambdaUpdateWrapper<Leave>();
		updateWrapper.eq(Leave::getOid, cancelLeaveRequest.getOid());
		Leave entity = new Leave();
		entity.setIscancel(true);
		leaveMapper.update(entity, updateWrapper);
		return responseComponent.toSuccess(null);
	}

}
